/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.matchme;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Bitmap.CompressFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.os.Environment;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Display;
import android.view.OrientationEventListener;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

// ----------------------------------------------------------------------

public class CameraPreview extends Activity implements PictureCallback, OnClickListener {    
    private Preview mPreview;
    private LinearLayout side1;
    private LinearLayout side2;
    
	public static final String IMAGE_FILE_NAME = "IMAGE_FILE_NAME";
    
    @Override
	protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // Hide the window title.
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    
        // Create our Preview view and set it as the content of our activity.
        mPreview = new Preview(this);
        setContentView(R.layout.camera_preview);
        mPreview = (Preview)findViewById(R.id.cameraPreview);
        side1 = (LinearLayout)findViewById(R.id.side1);
        side2 = (LinearLayout)findViewById(R.id.side2);
        
        WindowManager w = getWindowManager(); 
        Display d = w.getDefaultDisplay(); 
        int width = d.getWidth(); 
        int height = d.getHeight();
        int sideLength = 0;
        if((height > width))
        {
        sideLength =  (height - width)/2;
        
        side1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, sideLength));
        side2.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, sideLength));
        }
        Log.d("OMG", "OMG " + width  + " " + height );
        mPreview.setOnClickListener(this);
    }

	@Override
	public void onPictureTaken(byte[] data, Camera camera) {
    	// TODO
		
		java.io.File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), System.currentTimeMillis() + ".jpg");
		
		try {
			java.io.FileOutputStream os = new FileOutputStream(file);
			
			os.write(data);
			os.flush();
			os.close();
			
			Intent in = new Intent();
			in.putExtra(CameraPreview.IMAGE_FILE_NAME, file.getAbsolutePath());
			this.setResult(RESULT_OK, in);
			this.finish();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			this.setResult(RESULT_CANCELED);
			this.finish();
		} 
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		mPreview.mCamera.takePicture(null, null, this);
		
	}
}

// ----------------------------------------------------------------------

class Preview extends SurfaceView implements SurfaceHolder.Callback {
    SurfaceHolder mHolder;
    Camera mCamera;
    
    Preview(Context context) {
        super(context);
        
        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }
    
    public Preview(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		   mHolder = getHolder();
	        mHolder.addCallback(this);
	        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	}
    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, acquire the camera and tell it where
        // to draw.
        mCamera = Camera.open();
        try {
           mCamera.setPreviewDisplay(holder);
        } catch (IOException exception) {
            mCamera.release();
            mCamera = null;
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // Surface will be destroyed when we return, so stop the preview.
        // Because the CameraDevice object is not a shared resource, it's very
        // important to release it when the activity is paused.
        mCamera.stopPreview();
        mCamera.release();
        mCamera = null;
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // Now that the size is known, set up the camera parameters and begin
        // the preview.
        Camera.Parameters parameters = mCamera.getParameters();
       /// parameters.setPreviewSize(w, h);
       // mCamera.setParameters(parameters);
        mCamera.startPreview();
    }
    
    public Camera getCamera() {
    	return mCamera;
    }
    
}
